package com.example.huwang.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.huwang.R;
import com.example.huwang.model.CaseBean;
import com.example.huwang.model.HistoryBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {

    private List<HistoryBean> dataList;

    private OnClikListener onClikListener;

    public HistoryAdapter(List<HistoryBean> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_history_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HistoryBean historyBean = dataList.get(position);
        holder.title.setText(historyBean.getTitle());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClikListener!=null){
                    onClikListener.onClick(view, position);
                }
            }
        });
        holder.userName.setText(historyBean.getName());
        holder.time.setText(historyBean.getTime());
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView userName;
        private TextView time;
        private ImageView delete;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            userName = itemView.findViewById(R.id.user_name);
            time = itemView.findViewById(R.id.time);
            delete = itemView.findViewById(R.id.delete);
        }
    }

    public void setOnClikListener( OnClikListener onClikListener) {
        this.onClikListener = onClikListener;
    }

    public interface OnClikListener {
        void onClick(View view, int position);
    }

}